package com.shuorikeji.huohuo.ui.home;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.android.material.imageview.ShapeableImageView;
import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.TjBook3Adapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.BookDetailInfoBean;
import com.shuorikeji.huohuo.bean.BookDetailInfoSubBean;
import com.shuorikeji.huohuo.bean.BookDetailMainBean;
import com.shuorikeji.huohuo.bean.BookPlInfoBean;
import com.shuorikeji.huohuo.bean.BookPlMainBean;
import com.shuorikeji.huohuo.bean.BookZjMainBean;
import com.shuorikeji.huohuo.bean.TjBookMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.common.Constant;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.DialogUtil;
import com.shuorikeji.huohuo.utils.JsonUtil;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.shuorikeji.huohuo.view.GridDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class BookDetailActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_share)
    ImageView ivShare;
    @BindView(R.id.tv_pl)
    TextView tvPlCount;
    @BindView(R.id.siv_plr_tx)
    ShapeableImageView sivPlrTx;
    @BindView(R.id.tv_pl_user_name)
    TextView tvPlrName;
    @BindView(R.id.rb_pl_star)
    RatingBar rbPlStar;
    @BindView(R.id.tv_pl_content)
    TextView tvPlContent;
    @BindView(R.id.tv_pl_time)
    TextView tvPlTime;
    @BindView(R.id.siv_fm)
    ShapeableImageView sivFm;
    @BindView(R.id.tv_name)
    TextView tvBookName;
    @BindView(R.id.tv_zz)
    TextView tvZz;
    @BindView(R.id.tv_tips)
    TextView tvBookTips;
    @BindView(R.id.tv_pf)
    TextView tvPf;
    @BindView(R.id.tv_read_people)
    TextView tvRrs;
    @BindView(R.id.tv_book_zs)
    TextView tvBookZs;
    @BindView(R.id.tv_desc)
    TextView tvBookDesc;
    @BindView(R.id.iv_jj_more)
    ImageView ivJjMore;
    @BindView(R.id.rv_tj_y_like)
    RecyclerView rvTjLike;
    @BindView(R.id.ll_pl_content)
    LinearLayout llPlContent;
    @BindView(R.id.ll_pl_count)
    LinearLayout llPlCount;
    @BindView(R.id.tv_tj_sx)
    TextView tvTjSx;
    @BindView(R.id.tv_zj)
    TextView tvAllZj;
    @BindView(R.id.tv_wj_zt)
    TextView tvWjZt;
    @BindView(R.id.tv_see_all_pl)
    TextView tvSeeAllPl;
    @BindView(R.id.iv_write_pl)
    ImageView ivWritePl;
    @BindView(R.id.tv_write_pl)
    TextView tvWritePl;
    @BindView(R.id.ll_ml)
    LinearLayout llMl;
    @BindView(R.id.tv_add_sj)
    TextView tvAddSj;
    @BindView(R.id.tv_jx_read)
    TextView tvJxRead;
    @BindView(R.id.nsv_main)
    NestedScrollView nsvMain;

    private int BOOK_ID;
    private List<BookPlInfoBean> plList = new ArrayList<>();//评论数据
    private BookDetailInfoSubBean bookInfoDta;
    private boolean bookJjIsZk = false;//书籍简介内容是否展开
    private List<BookDetailInfoBean> yLikeBookList = new ArrayList<>();
    private TjBook3Adapter tjBook3Adapter;
    private int plTotalCount;
    private BookDetailInfoSubBean curReadingBookInfo;//当前正在读的书籍信息
    private int totalZjCount;
    private Dialog shareDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_book_detail);
    }

    @Override
    protected void initData() {
        curReadingBookInfo = BusinessUtils.getCurReadingBookInfo();
        initAdapter();
        getCYouLike(false);
        initIntent();
        initClick();
    }

    private void initAdapter() {
        rvTjLike.setLayoutManager(new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false));
        rvTjLike.addItemDecoration(new GridDividerItemDecoration(60));
        tjBook3Adapter = new TjBook3Adapter(BookDetailActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookDetailInfoBean data = (BookDetailInfoBean) object;
                        BOOK_ID = data.getId();
                        getBookPl();
                        break;
                }
            }
        });
        rvTjLike.setAdapter(tjBook3Adapter);
    }

    /**
     * 推荐你的喜欢
     */
    private void getCYouLike(boolean show) {
        mPresenter.cYouLike(BookDetailActivity.this, 1, 100, show, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        TjBookMainBean data = (TjBookMainBean) object;
                        yLikeBookList.clear();
                        yLikeBookList.addAll(data.getData().getData());
                        tjBook3Adapter.setData(yLikeBookList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(BookDetailActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void addSj() {
        mPresenter.addSj(BookDetailActivity.this, BOOK_ID, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        ToastShowUtil.showToastCenter(BookDetailActivity.this, "已添加书架");
                        if (bookInfoDta != null) {
                            bookInfoDta.setBookshelf(1);
                            tvAddSj.setBackground((bookInfoDta.getBookshelf() == 1) ? getResources().getDrawable(R.drawable.shape_4_eff1f5)
                                    : getResources().getDrawable(R.drawable.shape_4_f9e141));
                            tvAddSj.setText((bookInfoDta.getBookshelf() == 1) ? "已添加书架"
                                    : "添加书架");
                            tvAddSj.setTextColor((bookInfoDta.getBookshelf() == 1) ? getResources().getColor(R.color.color_C6C6C6)
                                    : getResources().getColor(R.color.color_333333));
                        }
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(BookDetailActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void jxRead() {
        Intent intent = null;
        if (curReadingBookInfo == null) {
            intent = new Intent(BookDetailActivity.this, BookMlActivity.class);
            intent.putExtra("BOOK_ID", BOOK_ID);
            intent.putExtra("PL_COUNT", plTotalCount);
            if (bookInfoDta != null) {
                intent.putExtra("BOOK_INFO", JsonUtil.toJson(bookInfoDta));
            }
        } else {
            if (BOOK_ID == curReadingBookInfo.getId()) {
                intent = new Intent(BookDetailActivity.this, ReadBookTempActivity.class);
                intent.putExtra("BOOK_ID", BOOK_ID);
                intent.putExtra("PL_COUNT", plTotalCount);
                intent.putExtra("ZJ_ID", curReadingBookInfo.getZjId());
                intent.putExtra("TOTAL_ZJ_COUNT", totalZjCount);
            } else {
                intent = new Intent(BookDetailActivity.this, BookMlActivity.class);
                intent.putExtra("BOOK_ID", BOOK_ID);
                intent.putExtra("PL_COUNT", plTotalCount);
                if (bookInfoDta != null) {
                    intent.putExtra("BOOK_INFO", JsonUtil.toJson(bookInfoDta));
                }
            }
        }
        startActivity(intent);
    }

    private void showShareDialog() {
        shareDialog = DialogUtil.showShareDialog(BookDetailActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        //QQ收藏
                        break;
                    case 2:
                        //微信好友
                        break;
                    case 3:
                        //微信收藏
                        break;
                }
            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        ivShare.setOnClickListener(view -> {
            showShareDialog();
        });
        tvTjSx.setOnClickListener(view -> {
            getCYouLike(true);
        });
        ivWritePl.setOnClickListener(view -> {
            writePl();
        });
        tvWritePl.setOnClickListener(view -> {
            writePl();
        });
        llMl.setOnClickListener(view -> {
            if (bookInfoDta != null) {
                Intent intent = new Intent(BookDetailActivity.this, BookMlActivity.class);
                intent.putExtra("BOOK_ID", BOOK_ID);
                intent.putExtra("BOOK_INFO", JsonUtil.toJson(bookInfoDta));
                intent.putExtra("PL_COUNT", plTotalCount);
                startActivity(intent);
            }
        });
        tvSeeAllPl.setOnClickListener(view -> {
            Intent intent = new Intent(BookDetailActivity.this, BookPlActivity.class);
            intent.putExtra("BOOK_INFO", JsonUtil.toJson(bookInfoDta));
            intent.putExtra("PL_COUNT", plTotalCount);
            startActivity(intent);
        });
        llPlCount.setOnClickListener(view -> {
            Intent intent = new Intent(BookDetailActivity.this, BookPlActivity.class);
            intent.putExtra("BOOK_INFO", JsonUtil.toJson(bookInfoDta));
            intent.putExtra("PL_COUNT", plTotalCount);
            startActivity(intent);
        });
        ivJjMore.setOnClickListener(view -> {
            if (bookJjIsZk) {
                bookJjIsZk = false;
                tvBookDesc.setMaxLines(4);
                ivJjMore.setImageResource(R.mipmap.icon_zk);
            } else {
                bookJjIsZk = true;
                tvBookDesc.setMaxLines(Integer.MAX_VALUE);
                ivJjMore.setImageResource(R.mipmap.icon_sh);
            }
        });
        tvAddSj.setOnClickListener(view -> {
            if (bookInfoDta == null) {
                addSj();
            } else {
                if (bookInfoDta.getBookshelf() == 1) {
                    ToastShowUtil.showToastCenter(BookDetailActivity.this, "已添加书架");
                } else {
                    addSj();
                }
            }
        });
        tvJxRead.setOnClickListener(view -> {
            jxRead();
        });
    }

    /**
     * 写评论
     */
    private void writePl() {
        Intent intent = new Intent(BookDetailActivity.this, WritePlActivity.class);
        intent.putExtra("BOOK_ID", BOOK_ID);
        startActivity(intent);
    }

    private void initIntent() {
        Intent intent = getIntent();
        BOOK_ID = intent.getIntExtra("BOOK_ID", -1);
        getBookPl();
    }

    private void getBookPl() {
        mPresenter.getBookPl(BookDetailActivity.this, 1, 1, BOOK_ID, 20, 1, -1, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookPlMainBean data = (BookPlMainBean) object;
                        plList.clear();
                        plList.addAll(data.getData().getData());
                        plTotalCount = data.getData().getTotal();
                        tvPlCount.setText(data.getData().getTotal() + " 点评");
                        if (plList != null && plList.size() > 0) {
                            llPlContent.setVisibility(View.VISIBLE);
                            BusinessUtils.setLoadImg(BookDetailActivity.this, plList.get(0).getUser().getAvatar(), sivPlrTx);
                            tvPlrName.setText(plList.get(0).getUser().getNickname());
                            if (plList.get(0).getStar() < 3) {
                                rbPlStar.setRating(1);
                            } else if (plList.get(0).getStar() < 5) {
                                rbPlStar.setRating(2);
                            } else if (plList.get(0).getStar() < 7) {
                                rbPlStar.setRating(3);
                            } else if (plList.get(0).getStar() < 9) {
                                rbPlStar.setRating(4);
                            } else {
                                rbPlStar.setRating(5);
                            }
                            tvPlContent.setText(plList.get(0).getContent());
                            tvPlTime.setText(plList.get(0).getCreatetime());
                        } else {
                            llPlContent.setVisibility(View.GONE);
                        }
                        getBookDetail();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(BookDetailActivity.this, msg);
                        getBookDetail();
                        break;
                }
            }
        });
    }

    private void getBookDetail() {
        mPresenter.getBookDetail(BookDetailActivity.this, BOOK_ID, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookDetailMainBean data = (BookDetailMainBean) object;
                        bookInfoDta = data.getData();
                        if (bookInfoDta == null) {
                            ToastShowUtil.showToastCenter(BookDetailActivity.this, "无数据");
                        } else {
                            showBookInfo();
                            nsvMain.fling(0);
                            nsvMain.smoothScrollTo(0, 0);
                        }
                        getBookZj();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(BookDetailActivity.this, msg);
                        getBookZj();
                        break;
                }
            }
        });
    }

    private void getBookZj() {
        mPresenter.getBookZj(BookDetailActivity.this, 1, 20, BOOK_ID, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookZjMainBean data = (BookZjMainBean) object;
                        totalZjCount = data.getData().getChapterList().getTotal();
                        tvAllZj.setText(data.getData().getChapterList().getTotal() + " 章节");
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(BookDetailActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void showBookInfo() {
        BusinessUtils.setLoadImg(BookDetailActivity.this, Constant.IMG_OSS_BASE_URL + bookInfoDta.getCover_image(), sivFm);
        tvBookName.setText(bookInfoDta.getName());
        tvBookTips.setText(BusinessUtils.getNewBookYxTag(bookInfoDta.getTag_id()) + "｜" +
                ((bookInfoDta.getPublish_status() == 1) ? "连载中" : "已完结"));
        tvWjZt.setText((bookInfoDta.getPublish_status() == 1) ? "连载中" : "已完结");
        tvPf.setText(bookInfoDta.getScore());
        tvRrs.setText(String.valueOf(bookInfoDta.getReading_num()));
        tvBookDesc.setText("简介:" + Html.fromHtml(bookInfoDta.getContent()));
        tvBookZs.setText(String.valueOf(bookInfoDta.getWord_num()));
        tvZz.setText(bookInfoDta.getAuthor());
        tvAddSj.setBackground((bookInfoDta.getBookshelf() == 1) ? getResources().getDrawable(R.drawable.shape_4_eff1f5)
                : getResources().getDrawable(R.drawable.shape_4_f9e141));
        tvAddSj.setText((bookInfoDta.getBookshelf() == 1) ? "已添加书架"
                : "添加书架");
        tvAddSj.setTextColor((bookInfoDta.getBookshelf() == 1) ? getResources().getColor(R.color.color_C6C6C6)
                : getResources().getColor(R.color.color_333333));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BusinessUtils.clearDialog(shareDialog);
    }
}